//
//  CZMainTabBar.swift
//  weibo
//
//  Created by Mac on 16/5/10.
//  Copyright © 2016年 fengzhixiang. All rights reserved.
//

import UIKit

class CZMainTabBar: UITabBar {

    // MARK: - 定义回调闭包属性
    /**
    3.加号按钮调用闭包
    */
    var composeCallback: (() -> ())?

    /**
     重写layoutSubviews方法,tabbar的button的位置
     */
    override func layoutSubviews() {
        super.layoutSubviews()
//        print("\(subviews)")

        //计算每个按钮的宽度
        let width = self.frame.size.width / 5
        var index = 0
        //遍历按钮
        for view in self.subviews{
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!){
                view.frame = CGRectMake(CGFloat(index) * width, 0, width, self.frame.size.height)
                index++

                if index == 2 {
                    index++
                }
            }
        }
        //设置自定义按钮在界面中间
        composeButton.frame = CGRectMake(2 * width, 0, width, self.frame.height)
    }


    //MARK: - 按钮点击事件,需要用闭包传递回调控制器
    func composeButtonClick(){
        // 最终需要去Modal出控制器,View不能Modal出控制器,只有控制器才能Modal出控制器,当按钮点击的时候需要让控制器去做事情

        //4.调用闭包
        composeCallback?()

    }






    //MARK: - 懒加载
    lazy var composeButton: UIButton = {
        //创建按钮(自定义Custom)
        let button = UIButton(type: UIButtonType.Custom)
        //设置按钮

        //设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)

        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_hightlighted"), forState: UIControlState.Highlighted)

        //按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)

        //添加到button
        self.addSubview(button)

        //添加加号按钮点击方法
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        //返回按钮
        return button
    }()

}
